﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace Mvc4Demo.Tests.Extensions
{
    internal class FakeDbSet<T> : IDbSet<T>
        where T:class , new()
    {
        public FakeDbSet(IEnumerable<T> collection)
        {
            Local = new ObservableCollection<T>(collection);
        }

        public FakeDbSet()
        {
            Local = new ObservableCollection<T>();
        }

        public IEnumerator<T> GetEnumerator()
        {
            return Local.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return Local.GetEnumerator();
        }

        #region IQueryable Members

        public Type ElementType
        {
            get { return Local.AsQueryable().ElementType; }
        }

        public Expression Expression
        {
            get { return Local.AsQueryable().Expression; }
        }

        public IQueryProvider Provider
        {
            get { return Local.AsQueryable().Provider; }
        }

        #endregion

        public T Find(params object[] keyValues)
        {
            return Local.FirstOrDefault();
        }

        public T Add(T entity)
        {
            Local.Add(entity);
            return entity;
        }

        public T Remove(T entity)
        {
            Local.Remove(entity);
            return entity;
        }

        public T Attach(T entity)
        {
            Local.Add(entity);
            return entity;
        }

        public T Create()
        {
            return new T();
        }

        public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T
        {
            throw new NotImplementedException();
        }

        public ObservableCollection<T> Local { get; private set; }
    }
}